package com.cc.dao.impl;

import com.cc.dao.BaseDao;
import com.cc.dao.employeeDao;
import com.cc.entity.employee;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class employeeImpl extends BaseDao implements employeeDao {

    @Override
    public List<employee> getAll(String empname) {
        List<employee> employeeList = new ArrayList<>();
        List<Object> params = new ArrayList<>();
        StringBuffer sb = new StringBuffer();
        sb.append(" SELECT e.*,d.dept_name from employee e");
        sb.append(" LEFT JOIN department d ON d.id = e.depld");
        if (empname != null && !"".equals(empname)) {
            sb.append(" AND e.empname LIKE '%" + empname + "%'");
        params.add(empname);
        }
//        sb.append("e.`start`=0");
        ResultSet rs = executeQuery(sb.toString(), params.toArray());
        try {
            while (rs.next()){
                employee employee=new employee();
                employee.setId(rs.getInt("id"));
                employee.setEmpname(rs.getString("empname"));
                employee.setPassword(rs.getString("password"));
                employee.setImage(rs.getString("image"));
                employee.setBirthday(rs.getDate("birthday"));
                employee.setSalary(rs.getDouble("salary"));
                employee.setDepld(rs.getString("depld"));
                employee.setCreateTime(rs.getDate("createTime"));
                employee.setUpdateTime(rs.getString("updateTime"));
                employee.setStart(rs.getString("start"));
                employeeList.add(employee);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            close(rs);
        }
        return employeeList;
    }
    @Override
    public int add(employee employee) {
        String sql="INSERT into employee (empname,depld,createTime,image,birthday,`start`)"+"VALUES(?,?,?,?,?,0)";
        Object[] parms={employee.getEmpname(),employee.getDepld(),employee.getCreateTime(),employee.getImage(),employee.getBirthday()};
        return executeUpdate(sql,parms);
    }
    @Override
    public employee getEdit(int id) {
        String sql = "select * from employee where id = ?";
        //存放条件的数组
        Object[] params = {id};
        System.out.println("pram:"+params[0]+",sql:"+sql);
        ResultSet rs = executeQuery(sql, params);
        System.out.println("结果："+rs);
        if (rs != null) {
            try {
                while (rs.next()) {
                    employee e = new employee();
                    e.setId(rs.getInt("id"));
                    e.setEmpname(rs.getString("empname"));
                    e.setPassword(rs.getString("password"));
                    e.setImage(rs.getString("image"));
                    e.setBirthday(rs.getDate("birthday"));
                    e.setSalary(rs.getDouble("salary"));
                    e.setDepld(rs.getString("depld"));
                    e.setCreateTime(rs.getDate("createTime"));
                    e.setUpdateTime(rs.getString("updateTime"));
                    e.setStart(rs.getString("start"));
                    return e;
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            } finally {
                close(rs);
            }
        }
        return null;
    }

    @Override
    public int UpdateEmq(employee e) {
        String sql = "update employee set empname = ?,password = ?,depld = ?,createTime = ?,image = ?,birthday = ? where id = ?";
        Object[] params = {e.getEmpname(), e.getPassword(), e.getDepld(), e.getCreateTime(), e.getImage(), e.getBirthday(), e.getId()};
        return executeUpdate(sql, params);
    }

    @Override
    public int delete(int e) {
        String sql = "delete from employee where id = ?";
        Object[] params = {e};
        return executeUpdate(sql, params);
    }

    @Override
    public List<employee> getPage(int pageNum, int pageSize, String empName) {
        List<employee> employeeList = new ArrayList<>();
        List<Object> params = new ArrayList<>();
        StringBuffer sb = new StringBuffer();
        sb.append(" SELECT e.*,d.dept_name from employee e");
        sb.append(" LEFT JOIN department d ON d.id = e.depld");
        sb.append(" WHERE 1=1");
        if (empName != null && !"".equals(empName)) {
            sb.append(" AND e.empname LIKE '%" + empName + "%'");
        }
//        sb.append("e.`start`=0");
        sb.append(" ORDER BY e.id DESC");
        sb.append(" LIMIT ?,?");
        ResultSet rs = executeQuery(sb.toString(), params.toArray());
        try {
            while (rs.next()){
                employee employee=new employee();
                employee.setId(rs.getInt("id"));
                employee.setEmpname(rs.getString("empname"));
                employee.setPassword(rs.getString("password"));
                employee.setImage(rs.getString("image"));
                employee.setBirthday(rs.getDate("birthday"));
                employee.setSalary(rs.getDouble("salary"));
                employee.setDepld(rs.getString("depld"));
                employee.setCreateTime(rs.getDate("createTime"));
                employee.setUpdateTime(rs.getString("updateTime"));
                employee.setStart(rs.getString("start"));
                employeeList.add(employee);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            close(rs);
        }
        return employeeList;
}

    @Override
    public int getTotalCount(String empName) {
        StringBuffer sb= new StringBuffer();
        sb.append(" SELECT COUNT(1) count FROM `employee` e");
        sb.append(" WHERE 1=1");
        if(empName !=null && !"".equals(empName)){
            sb.append("AND e.empname like '%" +empName +"%'");
        }
        ResultSet rs=executeQuery(sb.toString(),null);
        if(rs!=null){
            try {
                if(rs.next()){
                    return rs.getInt("count");
                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        return 0;
    }

}